/*
 * @Author: your name
 * @Date: 2022-04-22 14:55:27
 * @LastEditTime: 2022-04-22 16:37:41
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \mallAdmin\src\app\main\used-store\used-marketing-manage\second-coupon-list\second-coupon-list.component.ts
 */
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { sencondCouponStatusArr } from '../used-marketing-manage.pipe';
import { PermissionService } from '../../../../service/permission.service';
import { SecondCouponService } from '../services/second-coupon.service';
import { CommidityLabelManageService } from '../../commidity-mamage/commidity-label-list/commidity-label-manage.service';

@Component({
  selector: 'app-second-coupon-list',
  templateUrl: './second-coupon-list.component.html',
  styleUrls: ['./second-coupon-list.component.scss']
})
export class SecondCouponListComponent implements OnInit {
  // 路由缓存恢复时
  static updateCache: any = null;

  searchOptions = [
    { type: 1, labelText: '优惠券名称', valueKey: 'name', value: '' },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'statusStr',
      value: [],
      multiple: true,
      selectOptions: [...sencondCouponStatusArr],
    }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  // 审核Modal
  isExamineVisible = false;
  examineItem: any = {};
  examineParams = {
    remark: ''
  };

  // 手动发券 Modal
  releaseVisible = false;
  releaseLoading = false;
  opreaItem: any = {};

  constructor(
    private message: NzMessageService,
    private router: Router,
    public permission: PermissionService,
    private services: SecondCouponService,
    private commidityLabelManageService: CommidityLabelManageService
  ) { }

  ngOnInit(): void {
    SecondCouponListComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      pageNum: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const res: any = await this.services.getCouponList(params);
    this.tableParams.loading = false;

    if (res.code === 0) {
      const { records, total } = res.data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  queryData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * 页面跳转
   * @param type 0：新增 1：编辑 2：查看 3：复制
   * @param item 
   */
  jumpPage(type: number, item?: any) {
    let url = type === 2 ? '/used-store/second-coupon-details' : '/used-store/second-coupon-edit';
    let obj: any = {
      type
    };
    if ([1, 2, 3].includes(type)) {
      obj.id = item.id
    }
    this.router.navigate([url],{
      queryParams: obj
    })
  }

  /**
   * 审核
   * @param item 
   * ----------------------------------------------------------------
   */
  showExamineModal(item: any) {
    this.examineParams.remark = '';
    this.examineItem = item;
    this.isExamineVisible = true;
  }

  // Type 1：通过 7：不通过
  async subExamine(type: number) {
    if (type === 7 && !this.examineParams.remark) {
      this.message.warning('请输入审核意见');
      return
    }
    let params = {
      id: this.examineItem.id,
      reviewRemark: this.examineParams.remark,
      status: type
    }
    const { code, message }: any = await this.services.examineCouponItem(params);

    if (code === 0) {
      this.isExamineVisible = false;
      this.message.success('操作成功');
      this.loadTable();
    } else {
      this.message.error(message);
    }
  }

  /**
   * 停止
   * @param item
   * ----------------------------------------------------------------
   */
  async stopItem(item: any) {
    const res: any = await this.services.stopCouponItem(item.id);
    if (res.code === 0) {
      this.message.success('操作成功');
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }

  /**
   * 删除
   * @param item
   * ----------------------------------------------------------------
   */
  async deleteItem(item: any) {
    const { code, message }: any = await this.services.deleteCouponItem(item.id);
    if (code === 0) {
      this.message.success('删除成功');
      this.loadTable();
    } else {
      this.message.error(message);
    }
  }

  /**
   * 手动发券 Modal
   * ----------------------------------------------------------------
   */
  showCouponModal(item: any) {
    this.opreaItem = item;
    this.releaseVisible = true;
  }

  submitManual(val: any) {
    this.loadTable();
  }

  // 生成商品标签
  async generateGoodLable(item: any) {
    let params = {
      couponId: item.id
    }
    
    const res: any = await this.commidityLabelManageService.addEditGoodsLabelData(params, 0);
    if (res.code === 0) {
      this.message.success('成功生成商品标签');
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }
}
